package com.jindy.module.statement.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.statement.domain.AccountStatementApprove;
import com.jindy.module.statement.domain.MachineRespVo;
import com.jindy.module.statement.service.impl.AccountStatementServiceImpl;
import com.jindy.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.module.statement.domain.AccountStatement;
import com.jindy.module.statement.service.IAccountStatementService;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.common.core.page.TableDataInfo;

/**
 * 对账单Controller
 *
 * @author ruoyi
 * @date 2022-10-27
 */
@RestController
@RequestMapping("/module/accountStatement")
public class AccountStatementController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(AccountStatementController.class);
    @Autowired
    private IAccountStatementService accountStatementService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询对账单列表
     */
    @PreAuthorize("@ss.hasPermi('module:accountStatement:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountStatement accountStatement)
    {
//        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        accountStatement.setUserId(sysUser.getUserId());

        startPage();
        List<AccountStatementApprove> list = accountStatementService.selectAccountStatementApproveList(accountStatement);
        return getDataTable(list);
    }


    /**
     * 查询对账单机器列表
     */
//    @PreAuthorize("@ss.hasPermi('module:accountStatement:machineList')")
    @GetMapping("/machineList")
    public TableDataInfo machineList(AccountStatement accountStatement)
    {
        startPage();
//        accountStatement.setOrderId(1L);
        List<Map<String,Object>> list = accountStatementService.selectMachineList(accountStatement);
        return getDataTable(list);
    }

    /**
     * 查询对账单费用明细列表
     */
//    @PreAuthorize("@ss.hasPermi('module:accountStatement:costList')")
    @GetMapping("/costList")
    public TableDataInfo costList(AccountStatement accountStatement)
    {
        startPage();
        List<AccountStatement> list = accountStatementService.selectCostList(accountStatement);
        return getDataTable(list);
    }

    /**
     * 导出对账单列表
     */
    @Log(title = "对账单", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('module:accountStatement:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountStatement accountStatement)
    {
//        //添加权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        accountStatement.setUserId(sysUser.getUserId());

        List<AccountStatement> list = accountStatementService.selectAccountStatementList(accountStatement);
        ExcelUtil<AccountStatement> util = new ExcelUtil<AccountStatement>(AccountStatement.class);
        util.exportExcel(response, list, "对账单数据");
    }

    /**
     * 获取对账单详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:accountStatement:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(accountStatementService.selectAccountStatementById(id));
    }

    /**
     * 新增对账单
     */
    @Log(title = "对账单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:accountStatement:add')")
    @PostMapping
    public AjaxResult add(@RequestBody AccountStatement accountStatement)
    {
        try {
            return accountStatementService.insertAccountStatement(accountStatement);
        } catch (Exception e) {
            LOGGER.error("新增对账单失败",e);
            return AjaxResult.error();
        }
    }

    /**
     * 修改对账单
     */
    @Log(title = "对账单", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:accountStatement:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody AccountStatement accountStatement)
    {
        return toAjax(accountStatementService.updateAccountStatement(accountStatement));
    }

    /**
     * 删除对账单
     */
    @Log(title = "对账单", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:accountStatement:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(accountStatementService.deleteAccountStatementByIds(ids));
    }
}
